package com.xtwy.repository;

import com.xtwy.entity.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @作 者: 侯建军
 * @日 期: 2018/7/29 11:57
 * @描 述: 菜单Dao
 * @公 司：山西迅腾伟业
 */
@Repository
public interface MenuRepository extends JpaRepository<Menu, Long> {
    @Query(nativeQuery =true ,value="SELECT  m.id as mid, m.p_id,m.name as mname,m.status,m.url,m.icon,m.sort,m.is_menu,m.level,m.code,m.update_time,m.create_time,m.creator,r.id,r.name,r.value " +
            "FROM t_role r,t_permission p,t_menu m " +
            "WHERE p.role_id=r.id and p.menu_id=m.id " +
            "and r.id in (:ids)")
    Set<Map> findByRoleIds(@Param(value = "ids")List<Integer> ids);

    /**
     * 通过角色id获取权限
     * @param roleId
     * @return
     */
    @Query(nativeQuery =true ,value="SELECT  m.id as mid, m.p_id,m.name as mname,m.status,m.url,m.icon,m.sort,m.is_menu,m.level,m.code,m.update_time,m.create_time,m.creator,r.id,r.name,r.value " +
            "FROM t_role r,t_permission p,t_menu m " +
            "WHERE p.role_id=r.id and p.menu_id=m.id " +
            "and r.id =?1")
    List<Map> findByRoleId(Long roleId);

    @Query(nativeQuery =true ,value="SELECT DISTINCT  m.id as mid, m.p_id,m.name as mname,m.status,m.url,m.method,m.icon,m.sort,m.is_menu,m.level,m.code,m.update_time,m.create_time,m.creator,r.id,r.name,r.value \n" +
            "FROM t_role r,t_permission p,t_menu m,t_user u,t_user_role ur \n" +
            "WHERE  u.id=ur.user_id and r.id=ur.role_id and r.id=p.role_id and p.menu_id=m.id\n" +
            "and u.id =?1")
    List<Map> findByUserId(Long userId);
}
